from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SKUImage, SKU
from meiduo_admin.serializers.image import SKUImagesSerializer, SKUSerializer
from meiduo_admin.utils import PageNum, ImagePermission


class SKUImagesView(ModelViewSet):
    serializer_class = SKUImagesSerializer
    queryset = SKUImage.objects.all()
    pagination_class = PageNum
    permission_classes = [IsAdminUser]
    permission_classes_by_action = {'create': [ImagePermission],
                                    'update': [ImagePermission],
                                    'destroy': [ImagePermission]
                                    }

    def get_permissions(self):
        try:
            # return permission_classes depending on `action`
            return [permission() for permission in self.permission_classes_by_action[self.action]]
        except KeyError:
            # action is not set return default permission_classes
            return [permission() for permission in self.permission_classes]

    def simple(self,request):
        # 获取商品sku的id和name
        skus = SKU.objects.all()

        serializer = SKUSerializer(skus,many=True)


        return Response(serializer.data)

    # def create(self, request, *args, **kwargs):
    #     # serializer = self.get_serializer(data=request.data)
    #     # serializer.is_valid(raise_exception=True)
    #     # self.perform_create(serializer)
    #     # headers = self.get_success_headers(serializer.data)
    #     # return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #
    #
    #
    #
    #     return Response({
    #         'id':img.id,
    #         'sku':img.sku.id,
    #         'image':img.image.url
    #     })